<?php
/**
 * Service of Customer
 *
 * @author fsqijy
 */
require_once BaseUtil::getModulePath("base")  .  '/services/BaseService.php';
require_once BaseUtil::getModulePath("common")  .  '/models/CampusesDao.php';
require_once BaseUtil::getModulePath("common")  .  '/models/AreaDao.php';
require_once BaseUtil::getModulePath ( "dbgrid" ) . '/DBGrid.php';
require_once BaseUtil::getModulePath ( "dbgrid" ) . '/DBGridConstant.php';
class CommonService extends BaseService {
    private $areaDao;
    private $campusDao;
    public function __construct () {
        parent::__construct();
        $this->areaDao = new AreaDao();
        $this->areaDao->setDb($this->db);

        $this->campusDao = new CampusesDao();
        $this->campusDao->setDb($this->db);
    }
    public function getCampusByDictId($areaDictId){
       return $this->campusDao->getCampusByDictId($areaDictId);
    }
    public function getAllProvince(){
        return $this->areaDao->getAllProvince();
    }
    public function getCampusByName($campusName){
        return $this->campusDao->getCampusByName($campusName);
    }
    
/*
 * Steve
 */
    public function getCampusById($campusId,$visible = 1){
    	return $this->campusDao->getCampusById($campusId,$visible);
    }
    
    public function addCampus($campus){
    	return $this->campusDao->insertData($campus);
    }
    
    public function updateCampus($campus,$campusId){
    	$this->campusDao->updateCampus($campus,$campusId);
    }
    
    public function deleteCampus($campusId){
    	$this->campusDao->deleteCampus($campusId);
    }
    
    public function getCampuses($view,$post){
    	$provinces = $this->areaDao->getAllProvince();
    	foreach($provinces as $province){
    		$provinceArr[$province['id']] = $province['title'];
    	}
    	
    	$dbGrid = new DBGrid( $view );
    	$dbGrid->setPost( $post );
    	$dbGrid->setPageTitle ( $this->_translate->translate ( "campusList" ) );
    	
    	$dbGrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT,"agent_campuses-id",$this->_translate->translate( "campusID" ));
    	$dbGrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT,"agent_campuses-area_name",$this->_translate->translate( "campusName" ));
    	$dbGrid->addOptionsFilter(DBGridConstant::FILTER_TYPE_SELECT,"agent_campuses-area_dict_id",$this->_translate->translate( "areaName" ),$provinceArr);
    	$dbGrid->addOptionsFilter(DBGridConstant::FILTER_TYPE_SELECT,"agent_campuses-visible",$this->_translate->translate( "visible" ),array("1"=>$this->_translate->translate ( "yesLabel" ),"0"=>$this->_translate->translate ( "noLabel" )));
    	$dbGrid->addOptionsFilter(DBGridConstant::FILTER_TYPE_SELECT,"agent_campuses-show_in_order",$this->_translate->translate( "showInOrder" ),array("1"=>$this->_translate->translate ( "yesLabel" ),"0"=>$this->_translate->translate ( "noLabel" )));
    	
    	$dbGrid->addFetchTable("agent_campuses", "*");
    	$dbGrid->addFetchTable("area_dicts as ad","ad.title as area_dict","ad.id=agent_campuses.area_dict_id");
    	$dbGrid->addValueFilter("agent_campuses-is_delete","0");
    	
    	$dbGrid->addFetchColumn("id",$this->_translate->translate ( "campusID" ),"edit");
    	$dbGrid->addFetchColumn("area_name",$this->_translate->translate ( "campusName" ));
    	$dbGrid->addFetchColumn("area_dict",$this->_translate->translate ( "areaName" ));
    	$dbGrid->addFetchColumn("visible",$this->_translate->translate ( "visible" ),"",array("0"=>$this->_translate->translate ( "noLabel" ),"1"=>$this->_translate->translate ( "yesLabel" )));
    	$dbGrid->addFetchColumn("show_in_order",$this->_translate->translate ( "showInOrder" ),"",array("0"=>$this->_translate->translate ( "noLabel" ),"1"=>$this->_translate->translate ( "yesLabel" )));
        $dbGrid->addFetchColumn("created",$this->_translate->translate ( "createDate" ));
    	$dbGrid->setModule ( '/customer/campus' );
        if(AAService::isAllowed("campus", "add")){
            $dbGrid->addModuleAction("add",$this->_translate->translate("addBtn"),"add");
        }
		return $dbGrid->render ();
    }
    
    public function getDictTitleById($dict_id){
    	return $this->areaDao->getDictTitleById($dict_id);
    }
}